package com.ikas.ai.server.module.alarm.controller;

import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.model.PageQuery;
import com.ikas.ai.server.aop.Pageable;
import com.ikas.ai.server.aop.SpecialPermission;
import com.ikas.ai.server.module.AbstractBean;
import com.ikas.ai.server.module.alarm.service.TripAlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author li.hexian
 * @menu 跳机告警历史记录
 */
@RestController
@RequestMapping("/tripAlarm")
public class TripAlarmController extends AbstractBean {
    @Autowired
    TripAlarmService tripAlarmService;

    /**
     * 消缺
     *
     * @param id
     * @return
     */
    @GetMapping("/solve")
    @SpecialPermission(value = {"alarm:solved"})
    public JsonResult solve(@RequestParam Long id) {
        tripAlarmService.solved(id, getUserName());
        return JsonResult.ok();
    }

    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    @GetMapping("/page")
    @Pageable(sortType = "desc", sortBy = "createTime")
    public JsonResult page(PageQuery page,
                           String machineNo,
                           String meteName,
                           Integer solved,
                           @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                           @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime
    ) {
        return JsonResult.ok(
                tripAlarmService.page(machineNo, meteName, solved, startTime, endTime)
        );
    }

    @GetMapping("/getDescriptions")
    public JsonResult getDescriptions() {
        return JsonResult.ok(tripAlarmService.getAlarmDescriptions());
    }

    /**
     * 跳机消缺后重新开启
     *
     * @param id
     * @return
     */
    @GetMapping("/reOpen")
    public JsonResult reOpenForTest(Long id) {
        tripAlarmService.reOpenForTest(id);
        return JsonResult.ok();
    }
}
